package com.hl.card.modular.pack.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hl.card.common.controller.BaseController;
import com.hl.card.common.persistence.model.Bibivip;
import com.hl.card.common.util.ResponseModel;
import com.hl.card.core.log.LogObjectHolder;
import com.hl.card.modular.pack.service.IBibivipService;
import com.hl.card.modular.system.service.ISysDicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * bibivip控制器
 *
 * @author coole
 * @Date 2018-09-21 15:05:31
 */
@RestController
@RequestMapping("/api/bibivip")
public class BibivipController extends BaseController {

    private String PREFIX = "/packBack/bibivip/";
    @Autowired
    private IBibivipService bibivipService;
    @Autowired
    ISysDicService sysDicService;
    /**
     * 跳转到bibivip首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "bibivip.html";
    }
    /**
     * 添加bibivip
     */
    @PostMapping("/bibivip_add")
    public Object bibivipAdd(Bibivip bibivip) {
        ResponseModel responseModel=new ResponseModel();
        try {
            BigDecimal bigDecimal=new BigDecimal("10000");
            Calendar calendar = Calendar.getInstance();
            Date date = new Date(System.currentTimeMillis());
            calendar.setTime(date);
            calendar.add(Calendar.YEAR, 1);
            date = calendar.getTime();
            bibivip.setBalance(bigDecimal);
            bibivip.setCreateTime(new Date());
            bibivip.setExpiryTime(date);
            bibivipService.insert(bibivip);
            responseModel.setStatus("0001");
            responseModel.setData("创建成功");

        }catch (Exception e){
            responseModel.setStatus("0000");
            responseModel.setData("创建失败");
            e.printStackTrace();
        }
        return responseModel;
    }

    /**
     * 修改bibivip
     */
    @RequestMapping("/bibivip_update/{bibivipId}")
    public String bibivipUpdate(@PathVariable Integer bibivipId, Model model) {
        Bibivip bibivip = bibivipService.selectById(bibivipId);
        model.addAttribute("item",bibivip);
        LogObjectHolder.me().set(bibivip);
        return PREFIX + "bibivip_edit.html";
    }

    /**
     * 获取bibivip列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return bibivipService.selectList(null);
    }

    /**
     * 新增bibivip
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Bibivip bibivip) {
        bibivipService.insert(bibivip);
        return SUCCESS_TIP;
    }

    /**
     * 删除bibivip
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer bibivipId) {
        bibivipService.deleteById(bibivipId);
        return SUCCESS_TIP;
    }
    /**
     * 修改bibivip
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Bibivip bibivip) {
        bibivipService.updateById(bibivip);
        return SUCCESS_TIP;
    }
    /**
     * bibivip详情
     */
    @RequestMapping(value = "/detail/{userid}")
    @ResponseBody
    public Object detail(@PathVariable("userid") String userid) {
        Wrapper<Bibivip> warp=new  EntityWrapper<>();
        warp.and("userid",userid);
        Bibivip bibivip = bibivipService.selectOne(warp);
        ResponseModel responseModel=new ResponseModel();
        if(null==bibivip){
            responseModel.setStatus("0000");
        }else{
            responseModel.setStatus("0001");
            responseModel.setData(bibivip);
        }
        return bibivip;
    }
    /**
     * bibivip剩余
     */
    @GetMapping (value = "/surplus")
    @ResponseBody
    public Object surplus() {
        String max="50";
        Map<String, Object> hkVipMax = sysDicService.selectListByCodeNo("HKVIP", "MAX");
        if(hkVipMax != null) {
            max = (String) hkVipMax.get("dicValue");
        }
        int total = Integer.parseInt(max);
        ResponseModel responseModel=new ResponseModel();
        responseModel.setStatus("0001");
        Wrapper<Bibivip> warp=new  EntityWrapper<>();
        warp.and("status",2);
        int count = bibivipService.selectCount(warp);
        responseModel.setData(total-count);
        return responseModel;
    }
}
